<script setup lang="ts">
import { onMounted, onUnmounted, ref, watch } from 'vue';

import { useResizeObserver } from '@vueuse/core';
import * as echarts from 'echarts';

const props = defineProps({
  chartData: {
    type: Object,
    default: () => ({}),
  },
  height: {
    type: Number,
    default: () => 0,
  },
});

const chartRef = ref<HTMLDivElement | null>(null);
let chart: echarts.ECharts | null = null;

const initChart = () => {
  if (!chartRef.value) return;

  chart = echarts.init(
    chartRef?.value.querySelector('#mixChart') as HTMLElement,
  );

  const option = {
    backgroundColor: 'transparent',
    title: {
      show: false,
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        lineStyle: {
          color: '#177ddc',
        },
      },
    },
    grid: {
      top: '15%',
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true,
    },
    xAxis: {
      type: 'category',
      data: props.chartData.xAxis,
      axisLine: {
        lineStyle: {
          color: '#177ddc',
        },
      },
      axisLabel: {
        color: '#fff',
      },
    },
    yAxis: {
      type: 'value',
      splitLine: {
        lineStyle: {
          color: 'rgba(23, 125, 220, 0.2)',
        },
      },
      axisLabel: {
        color: '#fff',
      },
    },
    series: [
      {
        name: '组织数据',
        type: 'bar',
        data: props.chartData.barData,
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: '#00f2fe' },
            { offset: 1, color: '#177ddc' },
          ]),
        },
        barWidth: '20%',
        emphasis: {
          itemStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: '#00c6fb' },
              { offset: 1, color: '#005bea' },
            ]),
          },
        },
      },
      {
        name: '总数据',
        type: 'line',
        data: props.chartData.lineData,
        symbolSize: 8,
        symbol: 'circle',
        smooth: true,
        lineStyle: {
          width: 3,
          shadowColor: 'rgba(0, 242, 254, .3)',
          shadowBlur: 10,
        },
        itemStyle: {
          color: '#00f2fe',
          borderWidth: 2,
        },
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: 'rgba(0, 242, 254, 0.3)' },
            { offset: 1, color: 'rgba(0, 242, 254, 0.1)' },
          ]),
        },
      },
    ],
  };

  chart.setOption(option);
};

// 监听数据变化
watch(
  () => props.chartData,
  () => {
    initChart();
  },
  { deep: true },
);

// 监听容器大小变化
useResizeObserver(chartRef, () => {
  chart?.resize();
});

onMounted(() => {
  initChart();
});

onUnmounted(() => {
  chart?.dispose();
});
</script>

<template>
  <div class="chart-container" ref="chartRef">
    <div style="height: 560px" class="chart" id="mixChart"></div>
  </div>
</template>

<style scoped>
.chart-container {
  width: 100%;
  border-radius: 4px;
}

.chart {
  width: 100%;
  padding: 12px;
}
</style>
